import { deleteByPrimaryKeyForProResAuth, selectByPrimaryKeyForProResAuth, insertForProResAuth, updateForProResAuth, deleteByMapForProResAuth,updateByMapForProResAuth, getOneForProResAuth,getAllForProResAuth,queryPageForProResAuth, countForProResAuth, insertBatchForProResAuth, deleteBatchForProResAuth,updateBatchForProResAuth,getProResourceTreeByLevelCode,getAllProResourceTreeByAuthID,getPageAuthByProResourceTreeId,queryPageForProAuthority,insertForProRes,updateForProRes,deleteByPrimaryKeyForProRes,updateChangeTargetNodeWithSourceNodeForProResource } from '@/services/system/api_proresauth';

export default {
    namespace: 'proresauth',

    state: {
        data: {
            list: [],
            pagination: {},
        },
        resTree: {
        	list: [],
        },
        checkedKeys: {
        	list: []
        },
        recorddata: {
            datarecord:{},
            message:null,
            success: false,
        },
    },

    effects: {
            *delete_by_primarykey_for_proresauth({ payload }, { call, put }) {
                const response = yield call(deleteByPrimaryKeyForProResAuth, payload);
                yield put({
                    type: 'deleteByPrimaryKeyForProResAuth',
                    payload: response,
                });
            },
            *select_by_primarykey_for_proresauth({ payload, callback }, { call, put }) {
                const response = yield call(selectByPrimaryKeyForProResAuth, payload);
                yield put({
                    type: 'selectByPrimaryKeyForProResAuth',
                    payload: response,
                });
                if (callback) callback();
            },
            *insert_for_proresauth({ payload, callback }, { call, put }) {
                const response = yield call(insertForProResAuth, payload);
                yield put({
                    type: 'insertForProResAuth',
                    payload: response,
                });
                if (callback) callback();
            },
            *update_for_proresauth({ payload, callback }, { call, put }) {
                const response = yield call(updateForProResAuth, payload);
                yield put({
                    type: 'updateForProResAuth',
                    payload: response,
                });
                if (callback) callback();
            },
            *delete_by_map_for_proresauth({ payload, callback }, { call, put }) {
                const response = yield call(deleteByMapForProResAuth, payload);
                yield put({
                    type: 'deleteByMapForProResAuth',
                    payload: response,
                });
                if (callback) callback();
            },
            *update_by_map_for_proresauth({ payload, callback }, { call, put }) {
                const response = yield call(updateByMapForProResAuth, payload);
                yield put({
                    type: 'updateByMapForProResAuth',
                    payload: response,
                });
                if (callback) callback();
            },
            *get_one_for_proresauth({ payload, callback }, { call, put }) {
                const response = yield call(getOneForProResAuth, payload);
                yield put({
                    type: 'getOneForProResAuth',
                    payload: response,
                });
                if (callback) callback();
            },
            *query_page_for_proresauth({ payload, callback }, { call, put }) {
                const response = yield call(queryPageForProResAuth, payload);
                yield put({
                    type: 'queryPageForProResAuth',
                    payload: response,
                });
                if (callback) callback();
            },
            *count_for_proresauth({ payload, callback }, { call, put }) {
                const response = yield call(countForProResAuth, payload);
                yield put({
                    type: 'countForProResAuth',
                    payload: response,
                });
                if (callback) callback();
            },
            *insert_batch_for_proresauth({ payload, callback }, { call, put }) {
                const response = yield call(insertBatchForProResAuth, payload);
                yield put({
                    type: 'insertBatchForProResAuth',
                    payload: response,
                });
                
                const responsedata = yield call(queryPageForProAuthority, {});
                yield put({
                	type: 'queryPageForProAuthority',
                	payload: responsedata,
                });
                if (callback) callback();
            },
            *delete_batch_for_proresauth({ payload, callback }, { call, put }) {
                const response = yield call(deleteBatchForProResAuth, payload);
                yield put({
                    type: 'deleteBatchForProResAuth',
                    payload: response,
                });
                if (callback) callback();
            },
            *update_batch_for_proresauth({ payload, callback }, { call, put }) {
                const response = yield call(updateBatchForProResAuth, payload);
                yield put({
                    type: 'updateBatchForProResAuth',
                    payload: response,
                });
                if (callback) callback();
            },
            *get_proresourcetree_by_levelcode({ payload, callback }, { call, put }) {
                const response = yield call(getProResourceTreeByLevelCode, payload);
                yield put({
                    type: 'getProResourceTreeByLevelCode',
                    payload: response,
                });
                if (callback) callback();
            },
            *get_all_proresourcerree_by_authid({ payload, callback }, { call, put }) {
                const response = yield call(getAllProResourceTreeByAuthID, payload);
                yield put({
                    type: 'getAllProResourceTreeByAuthID',
                    payload: response,
                });
                if (callback) callback(response);
            },
            *get_page_auth_by_proresourcetreeid({ payload, callback }, { call, put }) {
                const response = yield call(getPageAuthByProResourceTreeId, payload);
                yield put({
                    type: 'getPageAuthByProResourceTreeId',
                    payload: response,
                });
                if (callback) callback();
            },
            *query_page_for_proauthority({ payload, callback }, { call, put }) {
                const response = yield call(queryPageForProAuthority, payload);
                yield put({
                    type: 'queryPageForProAuthority',
                    payload: response,
                });
                if (callback) callback();
            },
            *insert_for_prores({ payload, callback }, { call, put }) {
                const response = yield call(insertForProRes, payload);
                yield put({
                    type: 'insertForProRes',
                    payload: response,
                });
                const newPayload = {
                	parentid: payload.root_parentid,
                };

                const responseData = yield call(getProResourceTreeByLevelCode, newPayload);
                yield put({
                    type: 'getProResourceTreeByLevelCode',
                    payload: responseData,
                });
                
                const responseAuth = yield call(queryPageForProAuthority, {});
                yield put({
                    type: 'queryPageForProAuthority',
                    payload: responseAuth,
                });
                if (callback) callback();
            },
            *update_for_prores({ payload, callback }, { call, put }) {
                const response = yield call(updateForProRes, payload);
                yield put({
                    type: 'updateForProRes',
                    payload: response,
                });
                                
                const newPayload = {
                	parentid: payload.root_parentid,
                };

                const responseData = yield call(getProResourceTreeByLevelCode, newPayload);
                yield put({
                    type: 'getProResourceTreeByLevelCode',
                    payload: responseData,
                });
                
                const responseAuth = yield call(queryPageForProAuthority, {});
                yield put({
                    type: 'queryPageForProAuthority',
                    payload: responseAuth,
                });
                if (callback) callback();
            },
            *delete_by_primarykey_for_prores({ payload, callback }, { call, put }) {
                const response = yield call(deleteByPrimaryKeyForProRes, payload);
                yield put({
                    type: 'deleteByPrimaryKeyForProRes',
                    payload: response,
                });
                          
                const newPayload = {
                	parentid: payload.root_parentid,
                };

                const responseData = yield call(getProResourceTreeByLevelCode, newPayload);
                yield put({
                    type: 'getProResourceTreeByLevelCode',
                    payload: responseData,
                });
                
                const responseAuth = yield call(queryPageForProAuthority, {});
                yield put({
                    type: 'queryPageForProAuthority',
                    payload: responseAuth,
                });
                if (callback) callback();
            },
            *update_change_target_node_with_source_node_pro_resource({ payload, callback }, { call, put }) {
                const response = yield call(updateChangeTargetNodeWithSourceNodeForProResource, payload);
                yield put({
                    type: 'updateChangeTargetNodeWithSourceNodeForProResource',
                    payload: response,
                });
                
        

                if (callback) callback(response);
            },
            },

    reducers: {
        deleteByPrimaryKeyForProResAuth(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        selectByPrimaryKeyForProResAuth(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        insertForProResAuth(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        updateForProResAuth(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        deleteByMapForProResAuth(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        updateByMapForProResAuth(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        getOneForProResAuth(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        queryPageForProResAuth(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        countForProResAuth(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        insertBatchForProResAuth(state, action) {
        	console.log(action.payload);
            return {
                ...state,
                data: action.payload,
            };
        },
        deleteBatchForProResAuth(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        updateBatchForProResAuth(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        getProResourceTreeByLevelCode(state, action) {
            return {
                ...state,
                resTree: action.payload,
            };
        },
        getAllProResourceTreeByAuthID(state, action) {
            return {
                ...state,
                checkedKeys: action.payload,
            };
        },
        getPageAuthByProResourceTreeId(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        queryPageForProAuthority(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        insertForProRes(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        updateForProRes(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        deleteByPrimaryKeyForProRes(state, action) {
            return {
                ...state,
                data: action.payload,
            };
        },
        updateChangeTargetNodeWithSourceNodeForProResource(state, action) {
            return {
                ...state,
                recorddata: action.payload,
            };
        },
    },
};
